import { Http2Stream } from "./Http2Stream";
import buffer from '@ohos.buffer';
import ManagerMap from "../ManagerMap";

export class ClientHttp2Stream extends Http2Stream {
  _handle;
  _reqId;

  constructor(session, handle, reqId, options) {
    super(session, options);
    this._handle = handle;
    this._reqId = reqId;
  }

  on(event: string, callback: (...args: any[]) => void) {
    ManagerMap.getInstance().onCallBackMap.set(this._reqId + event, callback);
  }

  write(buf: buffer.Buffer, flags: number, ca: string, cert: string, key: string) {
    let arrayBuffer: ArrayBuffer = new ArrayBuffer(buf.length);
    let arr: Uint8Array = new Uint8Array(arrayBuffer);
    for (let i: number = 0; i < buf.length; i++) {
      arr[i] = buf[i];
    }
    this._handle.write(this._reqId, arrayBuffer, flags + "", ca, cert, key);
  }

  resume() {
  }

  pause() {
  }

  destroyed() {
  }

  end() {

  }

  close(code?: number): void {
    //主动关闭流
    if(code == 10000){
      ManagerMap.getInstance().onCloseCallBackMap.set(this._handle, true)
      this._handle.end(this._reqId);
    }
  }

  ping(buffer: string) {
  }

  getWindowSize(): string {
    return "";
  }

  getHandle(): any {
    return this._handle;
  }
}